<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="课题名称" prop="ktName">
        <el-input size="mini"
          v-model="queryParams.ktName"
          placeholder="请输入课题名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课题编号" prop="ktBh">
        <el-input size="mini"
          v-model="queryParams.ktBh"
          placeholder="请输入课题编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="processStatus">
        <el-select v-model="queryParams.processStatus" placeholder="请选择状态" clearable size="mini">
          <el-option
            v-for="dict in dict.type.process_business_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="委托单位" prop="wtDw">
        <el-input size="mini"
          v-model="queryParams.wtDw"
          placeholder="请输入委托单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上报年度" prop="sbNd">
        <el-date-picker clearable size="mini"
                        v-model="queryParams.sbNd"
                        type="year"
                        value-format="yyyy"
                        placeholder="请选择上报年度">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zxgl:zxtb:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zxgl:zxtb:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zxgl:zxtb:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zxgl:zxtb:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['zxgl:zxtb:import']"
        >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="!selectedRow || (selectedRow.processStatus !== 1 && selectedRow.processStatus !== 4)"
          @click="handleStartTask"
        >发起申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-delete"
          @click="handleRevokeProcess"
          :disabled="!selectedRow || selectedRow.processStatus !== 2"
        >撤销流程</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              v-loading="loading" :data="zxtbList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" width="50"/>
      <el-table-column label="上报年度" align="center" prop="sbNd" width="80"/>
      <el-table-column show-overflow-tooltip label="阶段时间" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ parseTime(scope.row.sbSsQTime, '{y}-{m}-{d}') + '~' + parseTime(scope.row.sbSsZTime, '{y}-{m}-{d}') }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="课题名称" align="center" prop="ktName"/>
      <el-table-column show-overflow-tooltip label="课题编号" align="center" prop="ktBh"/>
      <el-table-column show-overflow-tooltip label="委托单位" align="center" prop="wtDw"/>
      <el-table-column show-overflow-tooltip label="项目负责人" align="center" prop="xmFzName"/>
      <el-table-column label="状态" align="center" prop="processStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.process_business_status" :value="scope.row.processStatus" />
        </template>
      </el-table-column>

      <el-table-column show-overflow-tooltip label="上报人员" align="center" prop="sbRy"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.processStatus==1 || scope.row.processStatus==4"
            v-hasPermi="['htbz:htbz:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.processStatus==1 || scope.row.processStatus==4"
            v-hasPermi="['htbz:htbz:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleJinDu(scope.row)"
            v-if="scope.row.processStatus !== 1"
          >查询进度</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改执行情况填报对话框 -->
    <vxe-modal :title="title" v-model="open" width="75%" showFooter show-zoom resize>
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="120px">

        <el-tabs tab-position="right" type="border-card" v-model="activeName">
          <el-tab-pane name="JbZl" label="基本资料">
            <div v-if="activeName === 'JbZl'" style="height: 75vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">基本资料</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="课题名称" prop="ktName">
                      <el-input disabled v-model="form.ktName" placeholder="请选择关联课题" @focus="selectHt">
                        <el-button slot="append" icon="el-icon-search" @click="selectHt" style="color: #1890ff">查询课题列表
                        </el-button>
                      </el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="课题编号" prop="ktBh">
                      <el-input v-model="form.ktBh" placeholder="请输入课题编号"/>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="委托单位" prop="wtDw">
                      <el-input v-model="form.wtDw" placeholder="请输入委托单位"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="项目负责人" prop="xmFzName">
                      <el-input v-model="form.xmFzName" placeholder="请输入项目负责人"/>
                    </el-form-item>
                  </el-col>
                </el-row>

                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="上报所属时间" prop="sbSsQTime">
                      <el-date-picker clearable style="width: 100%;"
                                      v-model="form.sbSsQTime"
                                      type="date"
                                      value-format="yyyy-MM-dd">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="至" prop="sbSsZTime">
                      <el-date-picker clearable style="width: 100%;"
                                      v-model="form.sbSsZTime"
                                      type="date"
                                      value-format="yyyy-MM-dd">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="上报人员" prop="sbRy">
                      <el-input v-model="form.sbRy" placeholder="请输入上报人员"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="上报年度" prop="sbNd">
                      <el-date-picker clearable style="width: 100%;"
                                      v-model="form.sbNd"
                                      type="year"
                                      value-format="yyyy"
                                      placeholder="请选择上报年度">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-form-item label="执行情况" prop="zxQk">
                  <el-input :rows="6" v-model="form.zxQk" type="textarea" placeholder="请输入内容"/>
                </el-form-item>
                <el-form-item label="经费使用情况" prop="jfSyQk">
                  <el-input :rows="6" v-model="form.jfSyQk" type="textarea" placeholder="请输入内容"/>
                </el-form-item>
                <el-form-item label="存在的问题" prop="czDWt">
                  <el-input :rows="6" v-model="form.czDWt" type="textarea" placeholder="请输入内容"/>
                </el-form-item>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane v-if="this.form.idWhere != null" name="zynr" label="项目主要内容">
            <div style="height: 75vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">课题背景</span>
                </div>
                <editor v-model="form.ktBj" :min-height="192"/>
              </el-card>
              <br>
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">研究内容</span>
                </div>
                <editor v-model="form.yjNr" :min-height="192"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane v-if="this.form.idWhere != null" name="jsndycxd" label="技术难点与创新点">
            <div style="height: 75vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">技术难点</span>
                </div>
                <editor v-model="form.jsNd" :min-height="192"/>
              </el-card>
              <br>
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">技术创新点</span>
                </div>
                <editor v-model="form.jsCxd" :min-height="192"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane v-if="this.form.idWhere != null" name="zyjszbjjjzb" label="主要技术指标及经济指标">
            <div style="height: 75vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">技术指标</span>
                </div>
                <editor v-model="form.jsZb" :min-height="192"/>
              </el-card>
              <br>
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">经济指标</span>
                </div>
                <editor v-model="form.jjZb" :min-height="192"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane v-if="this.form.idWhere != null" name="fjzl" label="附件资料">
            <div v-if="activeName === 'fjzl'" style="height: 75vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">附件资料</span>
                </div>
                <el-row :gutter="10" class="mb8">
                  <el-col :span="1.5">
                    <!-- 上传按钮 -->
                    <el-button size="mini" icon="el-icon-upload" type="primary" @click="zxTbFjUpload">上传</el-button>
                  </el-col>
                  <el-col :span="1.5">
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteZxglZxtbFj">删除
                    </el-button>
                  </el-col>
                </el-row>
                <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}" height="60vh"
                          :data="zxglZxtbFjList" @selection-change="handleZxglZxtbFjSelectionChange" ref="zxglZxtbFj">
                  <el-table-column type="selection" width="50" align="center"/>
                  <el-table-column label="序号" align="center" type="index" width="50"/>
                  <el-table-column label="文件名称" align="center" prop="fileName" :formatter="getFileNames"/>
                  <el-table-column label="文件大小" align="center" prop="fileSize" width="200"/>
                  <el-table-column label="操作" align="center" width="150">
                    <template slot-scope="scope">
                      <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleDownload(scope.row)"
                      >下载
                      </el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-card>
            </div>
          </el-tab-pane>

        </el-tabs>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>

    <!-- 执行填报附件上传弹窗 -->
    <vxe-modal title="附件上传" v-model="fjUpload" width="25%" showFooter show-zoom resize @close="fjClose">
      <el-upload
        align="center"
        class="upload-demo"
        multiple
        :action="uploadFileUrl"
        :before-upload="handleBeforeUpload"
        :file-list="fileList"
        :limit="20"
        :on-error="handleUploadError"
        :on-exceed="handleExceed"
        :on-success="handleUploadSuccess"
        :headers="headers"
        ref="fileUpload"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          请上传
          <template> 大小不超过 <b style="color: #f56c6c">{{ 50 }}MB</b></template>
          <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b></template>
          的文件
        </div>
      </el-upload>

      <div slot="footer" class="dialog-footer" style="display: flex; justify-content: center;">
        <el-button v-if="allFilesUploaded === false" size="mini" type="primary" @click="fjUpload = false">关 闭
        </el-button>
        <el-button v-if="allFilesUploaded === true" size="mini" type="primary" @click="fjClose">上传完成, 关闭
        </el-button>
      </div>

    </vxe-modal>

    <!-- 执行填报导入组件 -->
    <ZxTbDr ref="zxTbDr"/>

    <!-- 合同单选组件 -->
    <HtDx ref="htDx"/>

    <!-- 进度组件 -->
    <Jindu ref="picture"/>

    <!-- 执行填报详情 -->
    <ZxTbXq ref="zxTbXq"/>
  </div>
</template>

<script>
import {
  addZxtb,
  delZxtb, delZxTbFj,
  getZxtb,
  getZxTbId,
  insertZxTbFjByZxTbId,
  listZxtb,
  selectZxTbFjList,
  updateZxtb
} from "@/api/xmgl/zxgl/zxtb";
import {getToken} from "@/utils/auth";
import ZxTbDr from "@/views/xmgl/zxgl/zxqktb/components/ZxTbDr";
import HtDx from "@/views/components/htXz/htDx";
import Jindu from "@/views/components/liuChengState/jindu";
import {revokeProcess, startTask} from "@/api/activiti/task";
import ZxTbXq from "@/views/xmgl/zxgl/zxqktb/components/ZxTbXq";

export default {
  name: "Zxtb",
  components: {ZxTbXq, Jindu, HtDx, ZxTbDr},
  dicts: ['process_business_status'],
  provide() {
    return {
      updateHtLabel: (val) => {
        this.form.ktName = val.htName //课题名称
        this.form.ktBh = val.htBh //课题编号
        this.form.wtDw = val.cdDw //委托单位
        this.form.xmFzName = val.fzName //项目负责人
      }
    };
  },
  data() {
    return {
      selectedRow: null, // 保存当前选中的行
      activeName: 'JbZl', //默认打开的标签
      xg: false, //判断是新增还是修改 (false新增 true修改)
      allFilesUploaded: false, // 添加这个来追踪所有文件是否上传完成
      fjUpload: false, //附件上传弹窗
      zxtbIdEdit: "", //要修改的 执行填报ID
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      fileList: [],
      headers: {Authorization: "Bearer " + getToken()},
      number: 0,
      uploadList: [],
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "docx", "zip"],
      getZxTbId: null, //后端获取的执行填报主键ID (UUID)
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedZxglZxtbFj: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 执行情况填报表格数据
      zxtbList: [],
      // 执行填报附件表格数据
      zxglZxtbFjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        idWhere: null,
        ktName: null,
        ktBh: null,
        wtDw: null,
        xmFzName: null,
        sbRy: null,
        sbNd: null,
        userId: this.$store.state.user.id,
        deptId: null,
        processStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ktName: [{required: true, message: "课题名称不能为空", trigger: "blur"}],
        ktBh: [{required: true, message: "课题编号不能为空", trigger: "blur"}],
        wtDw: [{required: true, message: "委托单位不能为空", trigger: "blur"}],
        xmFzName: [{required: true, message: "项目负责人不能为空", trigger: "blur"}],
        sbSsQTime: [{required: true, message: "上报所属时间不能为空", trigger: "blur"}],
        sbSsZTime: [{required: true, message: "上报所属时间不能为空", trigger: "blur"}],
        sbRy: [{required: true, message: "上报人员不能为空", trigger: "blur"}],
        sbNd: [{required: true, message: "上报年度不能为空", trigger: "blur"}],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //发起任务
    handleStartTask() {
      const row = this.selectedRow; // 获取当前选中的行
      if (!row) {
        this.$message.error("请先选择一行数据");
        return;
      }
      this.$confirm("是否确认发起申请？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          startTask({ businessKey: row.zxtbId, businessType: 6 }).then(res => {
            this.getList();
            this.$modal.msgSuccess("发起成功");
            this.selectedRow = null
          });
        })
        .catch(() => {});
    },
    //撤销流程
    handleRevokeProcess() {
      const row = this.selectedRow; // 获取当前选中的行
      if (!row) {
        this.$message.error("请先选择一行数据");
        return;
      }
      this.$confirm("是否确撤销申请？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let params = {
            businessKey: row.zxtbId,
            businessType: 6,
            processInstanceId: row.processInstanceId
          };
          revokeProcess(params).then(res => {
            this.getList();
            this.$modal.msgSuccess("撤销成功");
            this.selectedRow = null
          });
        })
        .catch(() => {});
    },
    handleButtonClick(row) {
      this.$refs.zxTbXq.handleOpen(row);
    },
    //查询进度
    handleJinDu(row) {
      this.$refs.picture.handleOpen(row);
    },
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    // 选择关联合同
    selectHt() {
      this.$refs.htDx.handleOpen();
    },
    zxTbFjUpload() { //执行填报附件上传按钮
      this.fjUpload = true;
    },
    //关闭上传附件弹窗后
    fjClose() {
      this.fileList = []
      this.allFilesUploaded = false
      this.fjUpload = false
    },
    // 文件下载处理
    handleDownload(row) {
      const resource = row.fileName;
      // 默认方法
      this.$download.resource(resource);
    },
    // 获取文件名原名
    getFileNames(row) {
      // 从路径中提取文件名
      return row.fileName.split('/').pop(); // 返回文件名部分
    },
    //上传文件之前的钩子
    handleBeforeUpload(file) {
      let that = this;
      // 校检文件类型
      if (this.fileType) {
        const fileName = file.name.split('.');
        const fileExt = fileName[fileName.length - 1];
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
          this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
          return false;
        }
        // 校检文件大小
        if (this.fileSize) {
          const isLt = file.size / 1024 / 1024 < this.fileSize;
          if (!isLt) {
            this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
            return false;
          }
        }
        this.$modal.loading("正在上传文件，请稍候...");
        this.number++;
        return true;
      }
    },
    // 对象转成指定字符串分隔
    listToString(list, separator) {
      let strs = "";
      separator = separator || ",";
      for (let i in list) {
        strs += list[i].url + separator;
      }
      return strs !== '' ? strs.substr(0, strs.length - 1) : '';
    },
    // 上传失败
    handleUploadError() {
      this.$modal.msgError("上传文件失败，请重试");
      this.$modal.closeLoading();
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    // 上传成功
    handleUploadSuccess(res, file) {
      let that = this;
      if (res.code === 200) {
        this.uploadList.push({name: res.fileName, url: res.fileName, size: that.formatFileSize(file.size)});
        let xzItemFile = {
          "zxtbId": that.getZxTbId,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        let xgItemFile = {
          "zxtbId": that.zxtbIdEdit,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        if (this.xg === false) {
          // 点击新增按钮的上传
          insertZxTbFjByZxTbId(xzItemFile).then(resFil => {
            that.getZxTbFjList();
          })
        } else if (this.xg === true) {
          // 点击修改按钮的上传
          insertZxTbFjByZxTbId(xgItemFile).then(resFil => {
            that.getZxTbFjList();
          })
        }
        this.uploadedSuccessfully();
      } else {
        this.number--;
        this.$modal.closeLoading();
        this.$modal.msgError(res.msg);
        this.$refs.fileUpload.handleRemove(file);
        this.uploadedSuccessfully();
      }
      this.allFilesUploaded = true
      this.$modal.msgSuccess("上传成功")
      // this.fjShangChuan = false
    },
    getZxTbFjList() {
      let that = this;
      if (this.xg === false) {
        //点击新增 上传文件后的文件列表
        selectZxTbFjList(that.getZxTbId).then(resFileList => {
          that.zxglZxtbFjList = resFileList.data;
        })
      } else {
        //点击修改 上传文件后的文件列表
        selectZxTbFjList(that.zxtbIdEdit).then(resFileList => {
          that.zxglZxtbFjList = resFileList.data;
        })
      }
    },
    // 显示文件大小为KB, MB, GB, TB
    formatFileSize(size) {
      if (size === 0) return '0 Bytes';
      const k = 1024;
      const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
      const i = Math.floor(Math.log(size) / Math.log(k));
      return parseFloat((size / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    },
    // 获取文件名称
    getFileName(name) {
      // 如果是url那么取最后的名字 如果不是直接返回
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1);
      } else {
        return name;
      }
    },
    // 上传结束处理
    uploadedSuccessfully() {
      if (this.number > 0 && this.uploadList.length === this.number) {
        this.fileList = this.fileList.concat(this.uploadList);
        this.fileList = this.fileList.map(item => {
          // 使用replace方法去除name中的特定前缀
          item.name = item.name.split('/').pop();
          return item;
        });
        this.uploadList = [];
        this.number = 0;
        this.$emit("input", this.listToString(this.fileList));
        this.$modal.closeLoading();
      }
    },
    /** 查询执行情况填报列表 */
    getList() {
      this.loading = true;
      listZxtb(this.queryParams).then(response => {
        this.zxtbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        zxtbId: this.getZxTbId,
        idWhere: null,
        ktName: null,
        ktBh: null,
        wtDw: null,
        xmFzName: null,
        sbSsQTime: null,
        sbSsZTime: null,
        sbRy: null,
        sbNd: null,
        zxQk: null,
        jfSyQk: null,
        czDWt: null,
        ktBj: null,
        yjNr: null,
        jsNd: null,
        jsCxd: null,
        jsZb: null,
        jjZb: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      };
      this.zxglZxtbFjList = [];
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.zxtbId)
      if (ids.length === 1) {
        this.ids = ids[0]
      } else {
        this.ids = ids
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getZxTbId().then(res => {
        this.getZxTbId = res.msg
        this.reset();
      })
      this.selectHt(); //打开合同选择弹窗组件
      this.activeName = 'JbZl'
      this.open = true;
      this.title = "添加执行情况填报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      this.xg = true
      const zxtbId = row.zxtbId || this.ids
      this.zxtbIdEdit = row.zxtbId || this.ids
      getZxtb(zxtbId).then(response => {
        this.form = response.data;
        this.zxglZxtbFjList = response.data.zxglZxtbFjList;
        this.activeName = 'JbZl'
        this.open = true;
        this.title = "修改执行情况填报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          this.form.zxglZxtbFjList = this.zxglZxtbFjList;
          if (this.form.idWhere != null) { //修改
            updateZxtb(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.xg = false
            });
          } else { //新增
            addZxtb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.handleUpdate(this.form)
              // this.open = false;
              // this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const zxtbIds = row.zxtbId || this.ids;
      this.$modal.confirm('确定删除选中记录？').then(function () {
        return delZxtb(zxtbIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 执行填报附件删除按钮操作 */
    handleDeleteZxglZxtbFj() {
      if (this.checkedZxglZxtbFj.length == 0) {
        this.$modal.msgError("请先选择要删除的执行填报附件数据");
      } else {
        const fjIds = this.checkedZxglZxtbFj;
        this.$modal.confirm('确定删除选中记录？').then(function() {
          return delZxTbFj(fjIds);
        }).then(() => {
          getZxtb(this.zxtbIdEdit).then(response => {
            this.zxglZxtbFjList = response.data.zxglZxtbFjList;
          });
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    },
    /** 复选框选中数据 */
    handleZxglZxtbFjSelectionChange(selection) {
      this.checkedZxglZxtbFj = selection.map(item => item.fjId)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zxgl/zxtb/export', {
        ...this.queryParams
      }, `zxtb_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.zxTbDr.handleImport();
    },
  }
};
</script>
